﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cleware.Debit3.Tools.EdmUtil
{
    using System.Data.SqlClient;

    class Program
    {
        static void Main(string[] args)
        {
            ProcessArgs(args);
        }
        static void ProcessArgs(string[] args)
        {
            Console.WriteLine("EdmUtil\n");
                
            bool showUsage = (args.Where(item => item == "?").Count() > 0) ||
                (args.Where(item => item == "/?").Count() > 0) ||
                (args.Where(item => item == "/h").Count() > 0);

            if (showUsage)
                ShowUsage();
            else
            {
                Parameters parameters = new Parameters();

                for (int i = 0; i < args.Length; i++)
                {
                    switch (args[i].ToLower())
                    {
                        case "/edmx":
                            if (++i < args.Length)
                                parameters.Edmx = args[i];
                            break;

                        case "/ssdl":
                            if (++i < args.Length)
                                parameters.Ssdl = args[i];
                            break;

                        case "/csdl":
                            if (++i < args.Length)
                                parameters.Csdl = args[i];
                            break;

                        case "/msl":
                            if (++i < args.Length)
                                parameters.Msl = args[i];
                            break;

                        case "/db":
                            if (++i < args.Length)
                                parameters.ConnectionString = args[i];
                            break;
                        case "/splist":
                            if (++i < args.Length)
                                parameters.SPList = new List<string>(args[i].Split(','));
                            break;
                        case "/exclusions":
                            if (++i < args.Length)
                                parameters.Exclusions = new List<string>(args[i].Split(','));
                            break;
                    }
                }

                string message;
                if (parameters.IsValid(out message))
                {
                    Edm edm = new Edm(parameters);
                    edm.Start();
                }
                else
                {
                    ShowUsage();
                    if (!string.IsNullOrEmpty(message))
                        Console.WriteLine(message);
                }
            }
        }
        
        static void ShowUsage()
        {
            StringBuilder sb = new StringBuilder("");
            sb.AppendLine("Usage: EdmUtil [/h] [/edmx edmx file] [/ssdl ssdl file] [/csdl csdl file] [/msl msl file] [/db database connection string] [/splist stored procedures] [/exclusions excl. sp]");
            Console.WriteLine(sb.ToString());
        }
    }
}
